/***************************************************************************
 *   Copyright (C) 2009 by Jason Pullara                                   *
 *   lordkat@gmail.com                                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "common.h"

cClient::cClient() {
	this->m_bIsNPC = true;
}

cClient::cClient(TCPsocket clientSocket, IPaddress clientAddress, cNetwork *pNetwork) {
	this->m_tcpSocket = clientSocket;
	this->m_ipAddress = clientAddress;
	this->m_clientState = CLIENT_MENU;
	this->m_clientType = CLIENTTYPE_TEXT;
	this->m_semClientLock = SDL_CreateSemaphore(1);
	this->m_bIsNPC = false;
	this->m_threadRecv = SDL_CreateThread(this->recvThread, (void *)this);
	this->m_pNetwork = pNetwork;
}

cClient::~cClient() {
	SDL_KillThread(m_threadRecv);
	SDL_DestroySemaphore(this->m_semClientLock);
	SDLNet_TCP_Close(this->m_tcpSocket);
	this->m_lRecvQueue.clear();
}

TCPsocket cClient::getTCPSocket() {
	SDL_SemWait(this->m_semClientLock);
	TCPsocket temp = this->m_tcpSocket;
	SDL_SemPost(this->m_semClientLock);
	return temp;
}

IPaddress cClient::getIPAddress() {
	SDL_SemWait(this->m_semClientLock);
	IPaddress temp = this->m_ipAddress;
	SDL_SemPost(this->m_semClientLock);
	return temp;
}

cClientState cClient::getClientState () {
	SDL_SemWait(this->m_semClientLock);
	cClientState temp = this->m_clientState;
	SDL_SemPost(this->m_semClientLock);
	return temp;
}

void cClient::setClientState(cClientState clientState) {
	SDL_SemWait(this->m_semClientLock);
	this->m_clientState = clientState;
	SDL_SemPost(this->m_semClientLock);
	return;
}

void cClient::setClientType(cClientType clientType) {
	SDL_SemWait(this->m_semClientLock);
	this->m_clientType = clientType;
	SDL_SemPost(this->m_semClientLock);
	return;
}

void cClient::sendData(string sBuffer) {
	SDL_SemWait(this->m_semClientLock);
	if (SDLNet_TCP_Send(this->m_tcpSocket, sBuffer.c_str(), sBuffer.length()) < sBuffer.length())
		this->setClientState(CLIENT_QUIT);
	SDL_SemPost(this->m_semClientLock);
	return;
}

string cClient::queueGet() {
	string sTemp;
	SDL_SemWait(this->m_semClientLock);
	if (!this->m_lRecvQueue.empty()) {
		sTemp = this->m_lRecvQueue.front();
		this->m_lRecvQueue.pop_front();
	}
	SDL_SemPost(this->m_semClientLock);
	return sTemp;
}

int cClient::recvThread(void *vData) {
	cClient *pClientTemp = ((cClient *)vData);
	string sTemp;
	while ((pClientTemp->m_iBufferLen = SDLNet_TCP_Recv(pClientTemp->m_tcpSocket, &pClientTemp->m_cBuffer, 10240)) > 0) {
		SDL_SemWait(pClientTemp->m_semClientLock);
		sTemp.clear();
		sTemp.assign(pClientTemp->m_cBuffer, pClientTemp->m_iBufferLen);
		pClientTemp->m_lRecvQueue.push_back(sTemp);
		SDL_SemPost(pClientTemp->m_semClientLock);
		SDL_Delay(1);
	}
	pClientTemp->setClientState(CLIENT_QUIT);
	return 0;
}
